#
# This Plan 9 mkfile is for ``easy'' building.
# It worked for Ghostscript 8.53.
#
# This mkfile consists of two parts: the first part builds the gs executable,
# the second unpacks the gs distribution and sets configuration parameters.
#
# Place this mkfile in the root directory of the gs distribution (the gsX.XX dir)
# Edit the drivers list below, and mk fake-make to let gs configure
# itself.  Fake-make doesn't actually build very much.
#
# Then you can run mk normally.
#
# Be sure to have the jpeg, libpng, and zlib subdirectories before starting.
# These have been included in recent gs tar files so we do not maintain
# our own anymore.
# 
# Each time you change the drivers list you must rerun mk fake-make
# before building.
#
# If you want to try out a new GS distribution, you should be able
# to mk try-X.XX, test it out, and then do mk install-X.XX, where X.XX
# is the version number (e.g. 5.70).  If outside BL, you may have to edit the
# scripts to remove some local dependencies (e.g. calling outside and the
# cpu call to thresher, alpha to make mips.h, alpha.h).
#
# Recent versions of src/gxshade1.c cause the compiler to run out
# of registers.  Brucee is looking into this.  In the meantime, use this
# replacement:
#
#	private inline void
#	make_other_poles(patch_curve_t curve[4])
#	{
#	    int i, j;
#	
#	    for (i = 0; i < 4; i++) {
#		j = (i + 1) % 4;
#		curve[i].control[0].x = (curve[i].vertex.p.x * 2 + curve[j].vertex.p.x);
#		curve[i].control[0].x /= 3;
#		curve[i].control[0].y = (curve[i].vertex.p.y * 2 + curve[j].vertex.p.y);
#		curve[i].control[0].y /= 3;
#		curve[i].control[1].x = (curve[i].vertex.p.x + curve[j].vertex.p.x * 2);
#		curve[i].control[1].y /= 3;
#		curve[i].control[1].y = (curve[i].vertex.p.y + curve[j].vertex.p.y * 2);
#		curve[i].control[1].y /= 3;
#		curve[i].straight = true;
#	    }
#	}
#	
# (the original includes the /3 in the big expressions).
#
# src/gxshade6.c also needs editing to remove use of the
# vlong , operator on some architectures.
#
# you may need to change this line in gs/src/gshtscr.c:
# //      sample = (int) (value * max_ht_sample) + max_ht_sample;
# to
# 	sample = (vlong)(value * max_ht_sample) + max_ht_sample;

</$objtype/mkfile

BIN=/$objtype/bin

TARG=gs
OFILES=\
	obj/gs.$O\
	`{sed 's#^./obj/(.*)\.o .*#obj/\1.$O#' src/ld.tr >[2] /dev/null | sort}

# The first driver is the default.
DRIVERS=\
	plan9\
	bj10e\
	bjc600\
	bjc800\
	cdj1600\
	cdj670\
	cdj850\
	cdj890\
	dfaxlow\
	epsonc\
	epswrite\
	inferno\
	jpeg\
	jpeggray\
	laserjet\
	ljet2p\
	ljet3\
	ljet3d\
	ljet4\
	pbm\
	pbmraw\
	pdfwrite\
	pgm\
	pgmraw\
	plan9bm\
	pnm\
	pnmraw\
	ppm\
	ppmraw\
	pswrite\
	stcolor\
	tiffg32d\
	tiffg3\
	tiffg4\

HFILES=$objtype.h\
	arch.h\

# enforce some startup conditions
x=`{mkdir obj >[2]/dev/null; mv jpeg/jmorecfg.h jpeg/jmorecfg.h.save >[2] /dev/null}


</sys/src/cmd/mkone

UPDATE=\
	/386/bin/gs\
	/sys/man/1/gs\
	/rc/bin/ps2pdf\
	/rc/bin/pdf2ps\
	/sys/man/1/ps2pdf\

update:V:
	update $UPDATEFLAGS $UPDATE `{cat lsr}


CC=pcc
CFLAGS=-c -DPlan9 -D_POSIX_SOURCE -D_BSD_EXTENSION\
	-I. -Isrc -Ijpeg -Izlib -Iicclib -DT$objtype '-DGS_INIT="gs_init.ps"'
LD=pcc

obj/gconfig.$O obj/gconf.$O obj/gscdef.$O\
 obj/gscdefs.$O obj/iconf.$O obj/iconfig.$O: src/gconfig.h

obj/%.$O: icclib/%.c
	$CC $CFLAGS icclib/$stem.c; mv $stem.$O obj

obj/%.$O:	jpeg/%.c
	$CC $CFLAGS jpeg/$stem.c; mv $stem.$O obj

obj/%.$O:	zlib/%.c
	$CC $CFLAGS zlib/$stem.c; mv $stem.$O obj

obj/%.$O:	src/%.c
	$CC $CFLAGS src/$stem.c; mv $stem.$O obj

#if possible, use genarch to build $objtype.h; it must run on same cpu type.
#if not, try to use a default header for the target architecture.
$objtype.h:	$O.genarch
	if(~ $objtype $cputype) {
		./$O.genarch $target
		rm -f $O.genarch
	}
	if not {
		if (test -r default.$objtype.h)
			cp default.$objtype.h $target && chmod 664 $target
		if not {
			echo 'no default header file for target architecture'
			echo 'run mk on the target architecture to build one'
			exit false
		}
	}

$O.genarch:U:	src/genarch.c
	if(~ $objtype $cputype) {
		rfork e
		objtype=$cputype
		pcc -DHAVE_LONG_LONG -B -o $O.genarch src/genarch.c
	}
	if not
		status=''

libinstall:V:
	cp lib/* /sys/lib/ghostscript

nuke clean:V:
	rm -f *.[$OS] obj/* [$OS].out y.tab.? y.debug y.output $TARG src/plan9.mak

#################################################################################

fake-make:V: src/plan9.mak src/plan9-aux.mak fakecc
	cp jpeg/jmorecfg.h.save jpeg/jmorecfg.h
	ape/psh -c 'CC=fakecc make -f src/plan9.mak'
	rm -f jpeg/jmorecfg.h
	cp obj/*.c obj/*.h obj/ld.tr src
	rm -f src/arch.h
	rm -f obj/*.o
	rm -f obj/*.c
	rm -f fakecc

real-make:V: src/plan9.mak src/plan9-aux.mak
	ape/psh -c 'CC=cc make -f src/plan9.mak'

fakecc:V:
	echo '#!/bin/rc
		if(~ ./obj/genconf $* || ~ ./obj/genarch $* || ~ ./obj/echogs $*) cc $*
		status='''' ' >$target
	chmod +x $target

src/plan9.mak:	src/unixansi.mak /sys/src/cmd/gs/contrib9.mak src/contrib.mak
	devlist='$(DD)'^$DRIVERS^'.dev'
	devlist=$"devlist
	sed 's#^GS_LIB_DEFAULT=.*#GS_LIB_DEFAULT=/sys/lib/ghostscript:/sys/lib/ghostscript/font:/sys/lib/postscript/font#
		s#^XCFLAGS=.*#XCFLAGS=-D_BSD_EXTENSION -DPlan9#
		s#^CFLAGS_STANDARD=.*#CFLAGS_STANDARD=#
		s#^include \$\(.*\)/(.*)#include src/\1#
		s#unix-aux.mak#plan9-aux.mak#
		s#gsdatadir *=.*#gsdatadir=/sys/src/cmd/gs#
		s#DEVICE_DEVS=.*#DEVICE_DEVS='$devlist'#
		s#(DEVICE_DEVS[0-9]+)=.*#\1=#' $prereq >$target

src/plan9-aux.mak:	src/unix-aux.mak
	sed 's#^INCLUDE=.*#INCLUDE=/sys/include/ape#' $prereq >$target

try-%:V:	gs%/$O.out
	bind gs$stem/$O.out /bin/gs
	bind -b gs$stem/lib /sys/lib/ghostscript
	echo 'give it a whirl'

gs([0-9]+\.[0-9]+)/$O.out:R:	gs\1/src
	@{ cd gs$stem1
	# for(i in jpeg zlib libpng)
	# 	bind -c ../$i $i
	if(! test -f src/ld.tr)
		mk fake-make
	cp ../arch.h .
	rm -f obj/*.c
	mk $O.out
	# for(i in jpeg zlib libpng)
	# 	unmount $i || status=''
	}

gs%/src:	ghostscript-%.tar.gz
	rm -rf gs$stem
	gunzip < ghostscript-$stem.tar.gz | tar x
	mv ghostscript-$stem gs$stem
	chmod g+w gs$stem/* gs$stem/*/*
	# local drivers
	cp src/gdevifno.c src/gdevplan9.c src/gdevcd8.c gs$stem/src
	# local conventions
	ed gs$stem/lib/gs_init.ps <fixSAFER.ed
	mkdir -p gs$stem/^(jpeg obj zlib libpng)
	# rm -rf gs$stem/^(jpeg obj zlib libpng)^/*
	echo '<../mkfile' >gs$stem/mkfile
	cp default.*.h gs$stem
	@{cd gs$stem; mk startover}

ghostscript-%.tar.gz:
	#outside
	#hget -o $target http://download.sourceforge.net/ghostscript/$target
	hget -o $target ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/AFPL/gs^`{echo $stem | sed 's/\.//'}^/$target
	#ftpfs -a $user^'@' ftp.cs.wisc.edu
	#cp /n/ftp/ghost/aladdin/*/ghostscript-$stem.tar.gz .

startover:V:
	rm -rf obj/*
	rm -f src/ld.tr
	rm -f src/plan9.mak src/plan9-aux.mak

install-%:V:
	unmount /bin/gs || status=''
	unmount /sys/lib/ghostscript || status=''
	mk installsrc-$stem
	cpu -h thresher -c 'mk mips.h' || status=''
	cpu -h alpha -c 'mk alpha.h' || status=''
	mk all
	mk installall
	for(i in lib/*.ps)
		if(! cmp $i /sys/lib/ghostscript/^`{basename $i} >/dev/null)
			cp $i /sys/lib/ghostscript
	cp /sys/lib/ghostscript/pdf_sec-ps.bak /sys/lib/ghostscript/pdf_sec.ps

installsrc-%:V:	gs%/$O.out
	pwd=`{pwd}
	# don't accidentally do this from gsX.XX
	if(! ~ $pwd */gs)
		echo 'i don''t think you meant to do that' && exit oops
	tocopy=(doc examples icclib lib man obj src zlib jpeg libpng)
	if(! cmp src/version.mak gs$stem/src/version.mak) {
		rm -rf $tocopy
		@{cd gs$stem; tar c $tocopy} | tar xT
		mk clean
	}
	status=''

clean-%:V:
	for (i in gs$stem/*)
		unmount $i >[2]/dev/null || status=''
	rm -rf gs$stem ghostscript-$stem.tar.gz

